﻿
using System;

using Microsoft.AspNetCore.Mvc;

using Xc.DataAccess.Core;
using Xc.DataAccess.Core.Extensions;
using Xc.DataAccess.DataBase.DeviceInfos;
using Xc.DataAccess.DataBase.DeviceInfos.Models;
namespace Xc.WaterMeters.ServerHosts.Controllers
{
    /// <summary>
    /// 命令信息管理服务
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CommandInfoController : ControllerBase
    {
        private readonly IDbCommandInfoService dbDeviceInfoService;
        /// <summary>
        /// 设备信息管理服务
        /// </summary>
        /// <param name="dbDeviceInfoService"></param>
        public CommandInfoController(IDbCommandInfoService dbDeviceInfoService)
        {
            this.dbDeviceInfoService = dbDeviceInfoService;
        }
        /// <summary>
        /// 获取设备的命令
        /// </summary>
        /// <param name="deviceInfoId"></param>
        /// <param name="days"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<CommandInfo[]> Gets(string deviceInfoId, int? days)
        {
            return dbDeviceInfoService.Gets(deviceInfoId, days);
        }
        /// <summary>
        /// 发送命令
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Result Push([FromBody]CommandInfoAdd commandInfoAdd)
        {
            if (commandInfoAdd is null)
            {
                return Result.Reject("无效的参数");
            }

            return  dbDeviceInfoService.Push(commandInfoAdd);
        }
        /// <summary>
        /// 获取最新待发送命令行
        /// </summary>
        /// <param name="deviceInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<CommandInfo, SendState> Peek(string deviceInfoId)
        {
            return dbDeviceInfoService.Peek(deviceInfoId);
        }
        /// <summary>
        /// 更新命令
        /// </summary>
        /// <param name="commandInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Update([FromBody]CommandInfo commandInfo)
        {
          return  dbDeviceInfoService.Update(commandInfo);
        }
        /// <summary>
        /// 撤销命令
        /// </summary>
        /// <param name="deviceInfoId"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result CancelCommands(string deviceInfoId)
        {
            return dbDeviceInfoService.CancelCommands(deviceInfoId);
        }
        /// <summary>
        /// 撤销指定命令
        /// </summary>
        /// <param name="deviceInfoId"></param>
        /// <param name="commandId"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result CancelCommand(Guid commandId)
        {
            return dbDeviceInfoService.CancelCommand(commandId);
        }
    }
}
